package cn.b.sky.mes.mm.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.PullType;
import cn.b.sky.mes.mm.model.condition.PullTypeCondition;
import cn.b.sky.mes.mm.service.PullTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : PullTypeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>物料拉动类型控制器<br> 
 * <strong>Create on : 2016年9月8日 下午5:48:21<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/pullType/*")
public class PullTypeController extends SecurityController {

  private static final String PULLING_TYPE_PAGE = "mm/pullType/typePage";

  @Autowired
  private PullTypeService pullTypeService;

  @RequestMapping("listPage")
  public String pullTypePage(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "mm/pullType/listPage";
  }

  @RequestMapping("getPullTypes")
  @ResponseBody
  public DWZResult getPullTypes(PullTypeCondition condition) {
    if (null == condition.getSiteCode()) {
      condition.setSiteCode(getSiteCode());
    }
    return ajaxDone(pullTypeService.getPullTypesByCondition(condition));
  }

  @RequestMapping("addPullType")
  public String addPullType(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute(PATTERN, "add");
    return PULLING_TYPE_PAGE;
  }

  @RequestMapping("modifyPullType")
  public String modifyPullType(String navTabId, Model model, String pullTypeCode) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute(PATTERN, "modify");
    PullType pullType = pullTypeService.getPullTypeByCode(getSiteCode(), pullTypeCode);
    model.addAttribute("pullType", pullType);
    return PULLING_TYPE_PAGE;
  }

  @RequestMapping("viewPullType")
  public String viewPullType(Model model, String pullTypeCode) {
    PullType pullType = pullTypeService.getPullTypeByCode(getSiteCode(), pullTypeCode);
    model.addAttribute("pullType", pullType);
    model.addAttribute(PATTERN, "view");
    return PULLING_TYPE_PAGE;

  }

  @RequestMapping("savePullType")
  @ResponseBody
  public DWZResult savePullType(PullType pullType, String navTabId) {
    if (null == pullType || StringUtils.isEmpty(pullType.getPullTypeCode())) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(pullType);
    if (pullType.getLockFlag() == null) {
      pullType.setLockFlag(0);
    }
    pullTypeService.savePullType(pullType);
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deletePullType")
  @ResponseBody
  public DWZResult deletePullType(String[] pullTypeCodes, HttpServletRequest request) {
    if (pullTypeCodes.length == 0) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }

    BatchJobReport[] reports = new BatchJobReport[pullTypeCodes.length];
    int failures = 0;
    for (int i = 0; i < pullTypeCodes.length; i++) {
      reports[i] = pullTypeService.deletePullTypeByCode(getSiteCode(), pullTypeCodes[i]);
      if (!reports[i].isSuccess()) {
        failures += 1;
      }
    }

    if (failures == 0) {
      return ajaxDone(null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("validatePullTypeCode")
  @ResponseBody
  public boolean validatePullTypeCode(String pullTypeCode) {
    if (!StringUtils.isEmpty(pullTypeCode)) {
      return null == pullTypeService.getPullTypeByCode(getSiteCode(), pullTypeCode);
    }
    return true;
  }
}
